/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package au.edu.unsw.bookmarket.action;

import au.edu.unsw.bookmarket.actionform.BookBean;
import au.edu.unsw.bookmarket.actionform.CourseBean;
import au.edu.unsw.bookmarket.actionform.DeleteDataBean;
import au.edu.unsw.bookmarket.actionform.PostAdsBean;
import au.edu.unsw.bookmarket.actionform.SellerBean;
import au.edu.unsw.bookmarket.service.BookService;
import au.edu.unsw.bookmarket.service.CourseService;
import au.edu.unsw.bookmarket.service.SellerService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author gongz
 */
public class DeleteDataAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    BookService bookService;
    CourseService courseService;
    SellerService sellerService;

    public void setBookService(BookService bookService) {
        this.bookService = bookService;
    }

    public void setCourseService(CourseService courseService) {
        this.courseService = courseService;
    }

    public void setSellerService(SellerService sellerService) {
        this.sellerService = sellerService;
    }

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        DeleteDataBean deleteDataBean = (DeleteDataBean) form;
        if (deleteDataBean.isRemoveAllBooks()) {
            bookService.removeAllBooks();
            List<BookBean> books = bookService.getAllBooks();
            System.out.println("There're " + books.size() + " books in DB after deletion!");
        }
        if (deleteDataBean.isRemoveAllCourses()) {
            courseService.removeAllCourses();
            List<CourseBean> courses = courseService.getAllCourses();
            System.out.println("There're " + courses.size() + " courses in DB after deletion!");
        }
        if (deleteDataBean.isRemoveAllSellers()) {
            sellerService.removeAllSellers();
            List<SellerBean> sellers = sellerService.getAllSellers();
            System.out.println("There're " + sellers.size() + " sellers in DB after deletion!");
        }
        return mapping.findForward(SUCCESS);
    }
}
